<template>
  <el-form label-width="100px" ref="formRef" :model="dataForm" :rules="dataRule">
    <el-form-item :label="i18n('user.username')" prop="username">
      {{ dataForm.username }}
    </el-form-item>

    <el-form-item :label="i18n('common.default_password')">
      <el-tag type="danger">123456</el-tag>
    </el-form-item>

    <el-form-item prop="role_id" :label="i18n('user.role.title')">
      <el-radio-group v-model="dataForm.role_id">
        <el-radio v-for="(v, k) in rolelist" :key="k" :label="v.id">
          {{ v.title }}
        </el-radio>
      </el-radio-group>
    </el-form-item>

    <el-form-item :label="i18n('user.nickname')" prop="nickname">
      <el-input v-model="dataForm.nickname" :placeholder="i18n('common.please_input') + i18n('user.nickname')"></el-input>
    </el-form-item>

    <el-form-item :label="i18n('user.avatar')" prop="avatar">
      <el-upload
        class="avatar-uploader"
        :action="$http.adornUrl('/file/picture')"
        :show-file-list="false"
        :headers="upload_headers"
        :on-success="handleAvatarSuccess"
        :before-upload="beforePictureUpload">
        <img v-if="dataForm.avatar" :src="dataForm.avatar" class="avatar-upload"/>
        <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
      </el-upload>
    </el-form-item>

    <el-form-item :label="i18n('user.mobile')" prop="mobile">
      <el-input v-model="dataForm.mobile" :placeholder="i18n('common.please_input') + i18n('user.mobile')"></el-input>
    </el-form-item>

    <el-form-item :label="i18n('user.email')" prop="email">
      <el-input v-model="dataForm.email" :placeholder="i18n('common.please_input') + i18n('user.email')"></el-input>
    </el-form-item>

    <el-form-item>
      <el-button v-if="isAuth('kernel:user:handle')" type="primary" @click="dataFormSubmit(formRef)">
        {{ i18n('common.confirm') }}
      </el-button>
      <el-button @click="resetForm(formRef)">
        {{ i18n('common.reset') }}
      </el-button>
    </el-form-item>
  </el-form>
</template>

<script setup name="account">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"
  import { useStore } from "@/hooks/store/useStore"

  const { user } = useStore()
  const { router, i18n, isAuth } = useBase()

  const rolelist = ref([])
  const dataForm = reactive({
    id: 0,
    role_id: '',
    username: '',
    nickname: '',
    avatar: '',
    mobile: '',
    email: '',
  })

  const dataRule = reactive({
    username: [{
      required: true,
      message: i18n('user.rules.username.require'),
      trigger: 'blur',
    }],
    nickname: [{
      required: true,
      message: i18n('user.rules.nickname.require'),
      trigger: 'blur',
    }],
    role_id: [{
      required: true,
      message: i18n('user.rules.role.require'),
      trigger: 'blur',
    }],
  })

  const {
    formRef,
    upload_headers,
    handleAvatarSuccess,
    beforePictureUpload,
    resetForm
  } = useForm('', dataForm)

  const init = (form) => {
    dataForm.id = user.user_id || 0

    nextTick(() => {
      form.value.resetFields()
      if (dataForm.id) {
        $http({
          url: $http.adornUrl(`/user/view/${dataForm.id}`),
          method: 'get',
          params: $http.adornParams({}),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            dataForm.role_id = data.data.role_id
            dataForm.username = data.data.username
            dataForm.nickname = data.data.nickname
            dataForm.avatar = data.data.avatar
            dataForm.mobile = data.data.mobile
            dataForm.email = data.data.email
          }
        })
      }
    })
  }


  // 表单提交
  const dataFormSubmit = (form) => {
    form.validate(async (valid, fields) => {
      if (valid) {
        $http({
          url: $http.adornUrl(`/user/handle`),
          method: 'post',
          data: $http.adornData({
            id: dataForm.id || undefined,
            role_id: dataForm.role_id,
            username: dataForm.username,
            nickname: dataForm.nickname,
            avatar: dataForm.avatar,
            mobile: dataForm.mobile,
            email: dataForm.email,
          }),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            $message.success(i18n('common.handle_success'))
            user.setUserInfo(dataForm)
            router.push({ name: 'home' })
          } else {
            $message.error(i18n(data.message))
          }
        })
      }
    })
  }


  const loadRoleList = () => {
    $http({
      url: $http.adornUrl('/role/select'),
      method: 'get',
    }).then(({ data }) => {
      if (data && data.status === 200) {
        rolelist.value = data.data
      } else {
        this.$message.error(i18n(data.message))
      }
    })
  }

  onMounted(() => {
    init(formRef)
    loadRoleList()
  })
</script>
